package com.example.AIstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.AIstudy.dao.ExerciseDao;
import com.example.AIstudy.domain.CuoTiJi;
import com.example.AIstudy.domain.Exercise;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.ExerciseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ExerciseServiceImpl extends ServiceImpl<ExerciseDao, Exercise> implements ExerciseService {

    @Autowired
    ExerciseDao exerciseDao;

    @Override
    public IPage<Exercise> findExercise(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);
        QueryWrapper<Exercise> qw = new QueryWrapper<>();
        //查询题目信息的时候可以筛选
        qw.eq(condition.containsKey("category"), "category", condition.get("category"));
        qw.like(condition.containsKey("problem"), "problem", condition.get("problem"));
        //直接查询试题信息的时候，不显示子题信息，点进去查看详情才显示
        qw.isNotNull( "sonProID").or().eq("isSingle", 1);
        return exerciseDao.findExercise(page, qw);
    }

    @Override
    @Transactional(rollbackFor = AIstudyException.class)
    public void insertExercise(List<Exercise>exercises) throws AIstudyException {
        try{
            int size=exercises.size();
            if(size==1){
                exerciseDao.insertExercise(exercises.get(0));
            }
            else{
                StringBuilder sonProID = new StringBuilder();
                //首先插入子题，获取每个子题的id，并且封装成一个字符串
                exerciseDao.insertExercise(exercises.get(1));
                sonProID.append(exercises.get(1).getId());
                for(int i=2;i<size;i++){
                    exerciseDao.insertExercise(exercises.get(i));
                    sonProID.append(","+exercises.get(i).getId());
                }
                //设置大题中第一题的子题字符串后再插入
                exercises.get(0).setSonProID(String.valueOf(sonProID));
                exerciseDao.insertExercise(exercises.get(0));
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new AIstudyException(AIstudyException.ErrorType.INSERT_ERROR,"添加习题失败");
        }
    }

    @Override
    public List<Exercise> findSonExercise(List<Integer> questionID) {
        return exerciseDao.selectBatchIds(questionID);
    }

    @Override
    @Transactional(rollbackFor = AIstudyException.class)
    public void updateExercise(List<Exercise>exercises) throws AIstudyException {

        try{
            int size=exercises.size();
            System.out.println("修改习题的数量是："+size);
            //逐个进行更新操作
            for(int i=0;i<size;i++){
                exerciseDao.updateById(exercises.get(i));
            }
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.UPDATE_ERROR,"修改习题信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = AIstudyException.class)
    public void deleteExercise(List<Integer> questionID) throws AIstudyException {
        try{
            exerciseDao.deleteBatchIds(questionID);
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.UPDATE_ERROR,"删除习题信息失败");
        }
    }

    //根据id获取全部匹配的题目
    @Transactional(rollbackFor = AIstudyException.class)
    public List<Exercise> getExerviceByesList(List<String>esids) throws AIstudyException{
        try{
            QueryWrapper<Exercise> qw = new QueryWrapper<>();
            qw.in("esID", esids);
            List<Exercise>exercises=exerciseDao.findExerciseByList(qw);
            int size=exercises.size();
            for(int i=0;i<size;i++){
                if(exercises.get(i).getCategory().equals("yuedu")){
                    //对阅读题进行封装
                    exercises.set(i,fengzhuangYuedu(exercises.get(i)));
                }
            }
            return exercises;
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.QUERY_ERROR,"获取习题信息失败");
        }
    }

    //对阅读题进行封装
    public Exercise fengzhuangYuedu(Exercise exercise){
        //获取子阅读题的ID
        String[] sonlistStr = exercise.getSonProID().split(",");
        List<Integer> sonList = new ArrayList<>();
        int sizeSon = sonlistStr.length;
        for (int j = 0; j < sizeSon; j++) {
            sonList.add(Integer.valueOf(sonlistStr[j]));
        }
        //获取子阅读题
        exercise.setSonExercise(exerciseDao.selectBatchIds(sonList));
        Exercise tmp=new Exercise();
        //设置用户id
        tmp.setId(exercise.getId());
        //设置答案
        tmp.setAnswer(exercise.getAnswer());
        //设置选项
        tmp.setChoice1(exercise.getChoice1());
        tmp.setChoice2(exercise.getChoice2());
        tmp.setChoice3(exercise.getChoice3());
        tmp.setChoice4(exercise.getChoice4());
        //设置选项个数
        tmp.setChoiceNum(exercise.getChoiceNum());
        //这是问题
        tmp.setProblem(exercise.getProblem());
        //将题目放到到第一道
        exercise.getSonExercise().add(0, tmp);
        return exercise;
    }

    public String selectSonProID(Integer exerciseID){
        return exerciseDao.selectSonProID(exerciseID);
    }

    public Exercise selectByesID(String esID){return exerciseDao.selectByesID(esID);}
}
